package com.nopay.cdxphone.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.nopay.cdxphone.bean.AreaBean;
import com.nopay.cdxphone.bean.response.AreaInfoResponse;

import java.util.ArrayList;
import java.util.List;
import com.nopay.cdxphone.R;

public class AreaAdapter extends BaseAdapter {

    private List<AreaBean> datas = new ArrayList();
    private LayoutInflater mInflater;
    private Context mContext;

    public AreaAdapter(Context context) {
        super();
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void addList(List<AreaBean> datas) {
        if(datas == null) return;

        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }

    public List<AreaInfoResponse.Data> getSelectedArea() {
        List<AreaInfoResponse.Data> datas = new ArrayList<>();
        for (int i = 0; i < getCount(); i++) {
            if(getItem(i).isSelected()) {
                if(getItem(i).isUnknown()) {
                    datas.add(null);
                } else {
                    for (int j = 0; j < getItem(i).getCityList().size(); j++) {
                        if(getItem(i).getCityList().get(j).isSelected()) {
                            AreaInfoResponse.Data data = new AreaInfoResponse.Data();
                            data.setProvince(getItem(i).getProvince());
                            data.setCity(getItem(i).getCityList().get(j).getName());
                            datas.add(data);
                        }
                    }
                }
            }
        }
        return datas.size() == 0 ? null : datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public AreaBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_area, null);
            holder.layout_province = convertView.findViewById(R.id.layout_province);
            holder.tv_area = convertView.findViewById(R.id.tv_area);
            holder.iv_area = convertView.findViewById(R.id.iv_area);
            holder.lv_city = convertView.findViewById(R.id.lv_city);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        AreaBean bean = getItem(position);
        holder.layout_province.setOnClickListener(v -> {
            if(onProvinceClickListener != null) onProvinceClickListener.onClick(v, position);
        });

        holder.tv_area.setText(bean.getProvince());
        holder.tv_area.setTextColor(Color.parseColor(bean.isSelected() ? "#178AED" : "#333333"));
        holder.iv_area.setVisibility(bean.isSelected() ? View.VISIBLE : View.INVISIBLE);
        AreaCityAdapter adapter = new AreaCityAdapter(mContext);
        holder.lv_city.setAdapter(adapter);
        adapter.addList(bean.getCityList());
        holder.lv_city.setOnItemClickListener((parent1, view, position1, id) -> {
            adapter.getItem(position1).setSelected(!adapter.getItem(position1).isSelected());
            adapter.notifyDataSetChanged();
        });
        holder.lv_city.setVisibility(bean.isSelected() ? View.VISIBLE : View.GONE);

        return convertView;
    }

    public class ViewHolder {
        private LinearLayout layout_province;
        private TextView tv_area;
        private ImageView iv_area;
        private ListView lv_city;
    }

    private OnProvinceClickListener onProvinceClickListener;

    public void setOnProvinceClickListener(OnProvinceClickListener onProvinceClickListener) {
        this.onProvinceClickListener = onProvinceClickListener;
    }

    public interface OnProvinceClickListener {

        void onClick(View view, int position);
    }
}
